package com.ym.controller.doctor.work;

import com.github.pagehelper.PageInfo;
import com.ym.common.constant.Constants;
import com.ym.core.annotation.ApiSign;
import com.ym.core.annotation.Locker;
import com.ym.core.annotation.Log;
import com.ym.core.domain.APIResponse;
import com.ym.core.domain.BaseController;
import com.ym.core.redis.RedisKeyEnum;
import com.ym.core.shiro.ShiroUtil;
import com.ym.medical.domain.entity.DoctorStudioEntity;
import com.ym.medical.domain.param.ConsultSummaryParam;
import com.ym.medical.domain.param.CounsellingRoomParam;
import com.ym.medical.domain.vo.CounsellingRoomRecordVO;
import com.ym.medical.service.IChatDetailsSV;
import com.ym.medical.service.IConsultSummarySV;
import com.ym.medical.service.ICounsellingRoomSV;
import com.ym.medical.service.IDoctorStudioSV;
import com.ym.medical.service.IDsAdviseRecordSV;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.constraints.NotNull;
import java.util.List;
import java.util.Map;

/**
 * 首页
 *
 * @author: 李涛
 * @version: 2019年05月14日 19:54
 */
@Api(tags = "医生端-首页-答复区")
@RestController
@ApiSign
@RequestMapping(value = "/doctor/work/reply",headers = Constants.Api.VERSION)
@Validated
public class ReplyController extends BaseController {

	@Autowired
	private ICounsellingRoomSV counsellingRoomService;

	@Autowired
	private IConsultSummarySV consultSummarySV;

	@Autowired
	private IChatDetailsSV chatDetailsSV;

	@Autowired
	private IDsAdviseRecordSV dsAdviseRecordSV;

	@Autowired
	private IDoctorStudioSV doctorStudioSV;

	@Log(dataBase = false, printBody = false)
	@ApiOperation("问诊列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "问诊类型：01当前问诊，02历史问诊", required = true, paramType = "query", dataType = "String"),
	})
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = CounsellingRoomRecordVO.class),
	})
	@GetMapping(value = "/the/inquiryList")
	public APIResponse<PageInfo<CounsellingRoomRecordVO>> theInquiryList(
			String type,
			@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize
	) {
		CounsellingRoomParam model = new CounsellingRoomParam();
		// 抢单医生ID
		model.setGrapDoctorId(ShiroUtil.getLoginId().toString());
		// 聊天室状态
		if ("01".equals(type)) {
			model.setCourStates("01,02");
			model.setIoStates("02");
		} else if ("02".equals(type)) {
			model.setIoStates("03,07");
		}
		PageInfo<CounsellingRoomRecordVO> counsellingRoomPageInfo = counsellingRoomService.theInquiryList(model, pageNum, pageSize);
		return successData(counsellingRoomPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("患者问诊记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "type", value = "问诊类型：01当前问诊，02历史问诊", required = true, paramType = "query", dataType = "String"),
	})
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = CounsellingRoomRecordVO.class),
	})
	@GetMapping(value = "/patient/consultation/record")
	public APIResponse<PageInfo<CounsellingRoomRecordVO>> patientConsultationRecord(
			@RequestParam Long patientId,
			@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize
	) {
		CounsellingRoomParam model = new CounsellingRoomParam();
		// 抢单医生ID
		model.setDoctorId(ShiroUtil.getLoginId());
		// 聊天室状态
		model.setIoStates("02,03");
		model.setPatientId(patientId);
		PageInfo<CounsellingRoomRecordVO> counsellingRoomPageInfo = counsellingRoomService.patientConsultationRecord(model, pageNum, pageSize);
		return successData(counsellingRoomPageInfo);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation("订单详情")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "orderId", value = "订单ID", required = true, paramType = "query", dataType = "String"),
	})
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = CounsellingRoomRecordVO.class),
	})
	@GetMapping(value = "/inquiry/info")
	public APIResponse<CounsellingRoomRecordVO> inquiryInfo(
			Long orderId
	) {
		CounsellingRoomParam model = new CounsellingRoomParam();
		model.setId(orderId);
		CounsellingRoomRecordVO counsellingRoomPageInfo = counsellingRoomService.inquiryInfo(model);
		return successData(counsellingRoomPageInfo);
	}


	@Log(dataBase = false, printBody = false)
	@ApiOperation("医生答复列表")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "types", value = "聊天室状态:01未答复02已答复03已关闭04未总结05已总结.多个逗号分隔", required = false, paramType = "query", dataType = "String"),
			@ApiImplicitParam(name = "patientId", value = "患者ID", required = false, paramType = "query"),
			@ApiImplicitParam(name = "ioStates", value = "订单状态多个", required = false, paramType = "query"),
			@ApiImplicitParam(name = "doctorId", value = "抢单人医生ID,为空查询自己", required = false, paramType = "query"),
			@ApiImplicitParam(name = "workDoctorId", value = "工作室医生ID,为空查询所有工作室的订单", required = false, paramType = "query"),
	})
	@ApiResponses({
			@ApiResponse(code = 200, message = "请求成功", response = CounsellingRoomRecordVO.class),
	})
	@GetMapping(value = "/inquiry/list")
	public APIResponse<PageInfo<CounsellingRoomRecordVO>> inquiryList(
			String types,
			String ioStates,
			Long patientId,
			Long doctorId,
			Long workDoctorId,
			@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize
	) {
		CounsellingRoomParam model = new CounsellingRoomParam();
		// 抢单医生ID
		if (doctorId == null) {
			doctorId = ShiroUtil.getLoginId();
		}
		model.setGrapDoctorId(doctorId.toString());
		if (workDoctorId != null) {
			model.setDoctorId(workDoctorId);
		}
		// 聊天室状态
		model.setCourStates(types);
		model.setPatientId(patientId);
		model.setIoStates(ioStates);
		PageInfo<CounsellingRoomRecordVO> counsellingRoomPageInfo = counsellingRoomService.listPageDoctorVisitRecord(model, pageNum, pageSize);
		return successData(counsellingRoomPageInfo);
	}


	@Log
	@ApiOperation("咨询总结添加")
	@Locker(paramExp = "0#courId?summaryUpdate", key = RedisKeyEnum.FREQUENT_OPERATION_KEY, limitTime = 1000, noGetMsg = "操作太频繁，请稍后再试！")
	@PostMapping(value = "/summary/update", produces = MediaType.APPLICATION_JSON_UTF8_VALUE, consumes = MediaType.APPLICATION_JSON_UTF8_VALUE)
	public APIResponse summaryUpdate(@RequestBody ConsultSummaryParam consultSummary
	) {
		boolean flag = consultSummarySV.summary(consultSummary, ShiroUtil.getLoginId());
		if (!flag) {
			return failMsg("咨询总结失败");
		}
		return successMsg("咨询总结成功");
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "获取双方聊天次数")
	@GetMapping("/getBothChats")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roomId", value = "咨询室ID主键", required = true, paramType = "query"),
	})
	public APIResponse<Long> getBothChats(
			@NotNull Long roomId
	) {
		// 患者发送次数
		Long chatsNums = chatDetailsSV.getChatsNums(roomId);
		return successData(chatsNums);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "获取双方聊天内容")
	@GetMapping("/getChatsContent")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "roomId", value = "咨询室ID主键", required = true, paramType = "query")
	})
	public APIResponse<Map> getChatsContent(
			@NotNull Long roomId,
			@RequestParam(defaultValue = "1") int pageNum,
			@RequestParam(defaultValue = "5") int pageSize
	) {

		Map<String, Object> result = chatDetailsSV.getChatsContent(roomId, pageNum, pageSize);
		return successData(result);
	}

	@Log(dataBase = false, printBody = false)
	@ApiOperation(value = "保存推荐专科医生记录")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "doctorId", value = "推荐的医生ID", required = true, paramType = "query"),
			@ApiImplicitParam(name = "orderId", value = "订单ID", required = true, paramType = "query")
	})
	@PostMapping(value = "/saveDoctorAdvise", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public APIResponse<List<Long>> saveDoctorAdvise(
			@NotNull Long[] doctorId,
			@NotNull Long orderId
	) {
		DoctorStudioEntity doctorStudio = doctorStudioSV.getCurrStudio();
		List<Long> adviseId = dsAdviseRecordSV.saveDoctorAdvise(orderId, doctorId, ShiroUtil.getLoginId(), doctorStudio == null ? null : doctorStudio.getId());
		return successData(adviseId);
	}

}
